package com.itboye.eweilisten.me.timu.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itboye.eweilisten.R;
import com.itboye.eweilisten.model.TIbean;
import com.itboye.eweilisten.util.sputil.Const;
import com.itboye.eweilisten.util.widget.XImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * 判断图片
 */
public class PanDuanImgAdapter extends RecyclerView.Adapter<PanDuanImgAdapter.MyViewHolder> {
    private Context context;
    private List<TIbean> beans;

    public PanDuanImgAdapter(Context context, List<TIbean> beans) {
        this.context = context;
        this.beans = beans;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_danxuan_img, parent, false);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public int getItemCount() {
        return beans.size();
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.danxuan_title.setText(beans.get(position).getTitle());
        XImageLoader.load(Const.IMAGE_HEAD + beans.get(position).getContent().get(0), holder.danxuan_image);
        holder.answerLine.removeAllViews();

        for (int j = 0; j < beans.get(position).getAnswers().size(); j++) {
            answerLineContentAdd(holder.answerLine, beans.get(position).getOption(), beans.get(position).getOption().get(j).getOptions(), j, position);
        }
    }

    /**
     * 答案
     * @param answerLine
     * @param string
     */
    private void answerLineContentAdd(LinearLayout answerLine, final ArrayList<TIbean.Options> qianzhui, String string, final int j, final int position) {
        // 设置添加的textvIew的参数
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.MATCH_PARENT,1.0f);
        params.setMargins(70, 5, 70, 5);
        final TextView textView = new TextView(context);
        textView.setText(string);
        textView.setTextSize(20);
        textView.setTextColor(Color.parseColor("#ffffff"));
        textView.setGravity(Gravity.CENTER);
        textView.setLayoutParams(params);
        textView.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.flag_04));
        answerLine.addView(textView);

        if (qianzhui.get(j).isDa()) {
            textView.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.flag_01));
        } else {
            textView.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.flag_04));
        }
        textView.setTag(R.id.tag, qianzhui.get(j).getOptions());
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = 0; i < qianzhui.size(); i++) {
                    qianzhui.get(i).setDa(false);
                }
                qianzhui.get(j).setDa(true);
                ArrayList<String> answer_truev = beans.get(position).getAnwser_true();
                int poistions = answer_truev.indexOf(textView.getTag(R.id.tag).toString());
                if (poistions == -1) {
                    beans.get(position).setXuan(1);//1代表选择错误
                } else {
                    beans.get(position).setXuan(0);//0代表选择正确
                }
                notifyDataSetChanged();
            }
        });
    }

    // 自定义的ViewHolder，持有每个Item的的所有界面元素
    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView danxuan_title;
        public ImageView danxuan_image;
        public LinearLayout answerLine;

        public MyViewHolder(View itemView) {
            super(itemView);
            danxuan_title = (TextView) itemView.findViewById(R.id.danxuan_title);
            danxuan_image = (ImageView) itemView.findViewById(R.id.danxuan_image);
            answerLine = (LinearLayout) itemView.findViewById(R.id.answerLine);
        }
    }
}
